package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.tces.entity.Clazz;
import com.eightgroups.tces.entity.ClazzVo;
import com.eightgroups.tces.entity.Dept;
import com.eightgroups.tces.service.IClazzService;
import com.eightgroups.tces.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;;import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author zzc2016.cn
 * @since 2017-11-06
 */
@Controller
@RequestMapping("/admin/clazz")
public class ClazzController extends BaseController {
	@Autowired
    private IClazzService iClazzService;
    @Autowired
    private IDeptService iDeptService;
    /**
     * 班级列表页面
     * @return
     */
    @GetMapping("/list")
    public String list(Model model) {
        EntityWrapper<Dept> entityWrapper = new EntityWrapper<>();
        List<Dept> depts = iDeptService.selectList(entityWrapper);
        model.addAttribute("depts",depts);
        return "/admin/clazz/list";
    }

    /**
     * 获取班级列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索名称
     * @return
     */
	@RequestMapping("/getList")
    @ResponseBody
    public Map<String, Object> getClazzList(int pageNumber, int pageSize, String searchText){
        Map<String,Object> result = new HashMap<String,Object>();
        Page<ClazzVo> clazzPage = new Page<>(pageNumber,pageSize);
        EntityWrapper<ClazzVo> entityWrapper = new EntityWrapper<>();
        entityWrapper.and("1=1");
        if(StringUtil.isNotEmpty(searchText)){
            entityWrapper.like("cno",searchText);
        }
        entityWrapper.orderBy("cno",true);
        Page<ClazzVo> page = iClazzService.selectByDept(clazzPage, entityWrapper);
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        return result;
    }

    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @RequestMapping("/addClazz")
    @ResponseBody
    public JsonResult addClazz(Clazz clazz){
        return iClazzService.insert(clazz)?renderSuccess("成功"):renderError("失败");
    }
    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @RequestMapping("/updateClazz")
    @ResponseBody
    public JsonResult updateClazz(Clazz clazz){
        return iClazzService.updateById(clazz)?renderSuccess("修改成功"):renderError("修改失败");
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(@RequestParam(value = "id", required = false) String id){
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        EntityWrapper<Clazz> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("id",id);
        return iClazzService.delete(entityWrapper)? renderSuccess("删除成功") : renderError("删除失败");
    }
}
